package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.enums.RedisKeyEnum;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.entity.MyCircle;
import zhihuilan.entity.MyCircleThumbup;
import zhihuilan.mapper.MyCircleThumbupMapper;
import zhihuilan.params.query.MyCircleCollectionQueryParam;
import zhihuilan.params.query.MyCircleThumbupQueryParam;
import zhihuilan.service.MyCircleThumbupService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyCircleThumbupServiceImpl extends ServiceImpl<MyCircleThumbupMapper, MyCircleThumbup> implements MyCircleThumbupService {

    @Resource
    private RedisCache redisCache;

    @Override
    public Page<MyCircleThumbup> selectPageList(MyCircleThumbupQueryParam queryParam) {
        QueryWrapper<MyCircleThumbup> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        queryWrapper.orderByDesc("create_time");
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        Page<MyCircleThumbup> page = new Page<>(pageNum, pageSize);
        this.baseMapper.selectPage(page, queryWrapper);
        return page;
    }

    @Override
    public Long selectCount(MyCircleThumbupQueryParam queryParam) {
        QueryWrapper<MyCircleThumbup> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.eq("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        return this.baseMapper.selectCount(queryWrapper);
    }


    @Override
    public Long selectCountToCricle(Integer staffId) {
        if (ObjectUtils.isEmpty(staffId)) {
            return 0L;
        }
        return this.baseMapper.selectCountToCircle(staffId);
    }

    @Override
    public MyCircleThumbup selectByPidAndStaffId(Integer pid, Integer staffId) {
        QueryWrapper<MyCircleThumbup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("staff_id", staffId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public MyCircleThumbup addEntity(MyCircleThumbup myCircleThumbup) {
        this.baseMapper.insert(myCircleThumbup);
        return myCircleThumbup;
    }

    @Override
    public Boolean deleteByPidAndStaffId(Integer pid, Integer staffId) {
        QueryWrapper<MyCircleThumbup> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("staff_id", staffId);
        int count = this.baseMapper.delete(queryWrapper);
        if (count > 0) {
            return true;
        } else {
            return false;
        }
    }
}
